/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generatepage.web.IdxHotSaleAdvAction
   Module Description   :

   Date Created      : 2010/3/4
   Original Author   : apple.chang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generatepage.web;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.ec.common.EcConstant;
import com.yaodian100.ec.generate.entity.GenerateAdvertise;
import com.yaodian100.ec.generate.entity.GenerateContent;
import com.yaodian100.ec.generate.entity.GeneratePosition;
import com.yaodian100.ec.generate.service.GenerateService;

public class IdxHotSaleAdvActionV2 extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4598684155164450511L;

	private final Logger logger = LoggerFactory.getLogger(IdxHotSaleAdvActionV2.class);
	private GenerateService generateService;
	private GenerateAdvertise advertise;
	private GeneratePosition position;
	private GenerateContent content;
	private StringBuilder contents;
	private String advId;
	private String advName;
	private String advType = "IDX_HOTSALE_V2";
	private String advStatus;
	private String startDate;
	private String endDate;
	private String startTime;
	private String endTime;

	private String tab1ProdId;
	private String tab1Name;
	private String tab1adWord;

	private String tab2ProdId;
	private String tab2Name;
	private String tab2adWord;

	private String tab3ProdId;
	private String tab3Name;
	private String tab3adWord;

	private String tab4ProdId;
	private String tab4Name;
	private String tab4adWord;
	private final DateFormat PATTERN_DATE_FORMAT = new SimpleDateFormat(EcConstant.PATTERN_TIMESTAMP);

	/**
	 * 新增首頁熱銷商品
	 * 
	 * @return
	 */
	public String addIdxHotSaleAdv() {
		advId = "";
		return "editIdxHotSaleAdv";
	}

	/**
	 * 编辑首页类别促销版位
	 * 
	 * @return
	 * @throws Exception
	 */
	public String editIdxHotSaleAdv() throws Exception {
		initVariables();
		advertise = generateService.get(Long.parseLong(advId));
		position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
		content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
		advStatus = advertise.getAdvStatus();
		advType = advertise.getAdvType();
		advName = advertise.getAdvName();
		startDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getStartDate());
		endDate = FastDateFormat.getInstance(EcConstant.PATTERN_DATE).format(advertise.getEndDate());
		startTime = FastDateFormat.getInstance("HH:mm").format(advertise.getStartDate());
		endTime = FastDateFormat.getInstance("HH:mm").format(advertise.getEndDate());
		readPropertise(content);
		return "editIdxHotSaleAdv";
	}

	/**
	 * @return the advId
	 */
	public String getAdvId() {
		return advId;
	}

	/**
	 * @return the advName
	 */
	public String getAdvName() {
		return advName;
	}

	/**
	 * @return the advStatus
	 */
	public String getAdvStatus() {
		return advStatus;
	}

	/**
	 * @return the advType
	 */
	public String getAdvType() {
		return advType;
	}

	/**
	 * @return the content
	 */
	public GenerateContent getContent() {
		return content;
	}

	/**
	 * @return the endDate
	 */
	public String getEndDate() {
		return endDate;
	}

	public String getEndTime() {
		return endTime;
	}

	/**
	 * @return the startDate
	 */
	public String getStartDate() {
		return startDate;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getTab1Name() {
		return tab1Name;
	}

	public String getTab1ProdId() {
		return tab1ProdId;
	}

	public String getTab2Name() {
		return tab2Name;
	}

	public String getTab2ProdId() {
		return tab2ProdId;
	}

	public String getTab3Name() {
		return tab3Name;
	}

	public String getTab3ProdId() {
		return tab3ProdId;
	}

	public String getTab4Name() {
		return tab4Name;
	}

	public String getTab4ProdId() {
		return tab4ProdId;
	}

	private boolean idxHotSaleAdvValidate() throws CoreException, ParseException, IOException {
		/**************************** 必填栏位 ****************************/
		if (StringUtils.isBlank(advName)) {
			addFieldError("advName", "请输入广告名称");
		}

		if (StringUtils.isBlank(startDate)) {
			addFieldError("startDate", "请输入起始日期");
		}

		if (StringUtils.isBlank(endDate)) {
			addFieldError("endDate", "请输入结束日期");
		}

		if (StringUtils.isBlank(tab1ProdId)) {
			addFieldError("tab1ProdId", "請輸入tab1商品编号");
		}

		if (StringUtils.isBlank(tab1Name)) {
			addFieldError("tab1Name", "請輸入tab1商品名称");
		}

		if (StringUtils.isBlank(tab2ProdId)) {
			addFieldError("tab2ProdId", "請輸入tab2商品编号");
		}

		if (StringUtils.isBlank(tab2Name)) {
			addFieldError("tab2Name", "請輸入tab2商品名称");
		}

		if (StringUtils.isBlank(tab3ProdId)) {
			addFieldError("tab3ProdId", "請輸入tab3商品编号");
		}

		if (StringUtils.isBlank(tab3Name)) {
			addFieldError("tab3Name", "請輸入tab3商品名称");
		}

		if (StringUtils.isBlank(tab4ProdId)) {
			addFieldError("tab4ProdId", "請輸入tab4商品编号");
		}

		if (StringUtils.isBlank(tab4Name)) {
			addFieldError("tab4Name", "請輸入tab4商品名称");
		}

		if (getFieldErrors().isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 初始化变数值
	 */
	void initVariables() {
		advertise = new GenerateAdvertise();
		position = new GeneratePosition();
		content = new GenerateContent();

		tab1ProdId = "";
		tab1Name = "";
		tab2ProdId = "";
		tab2Name = "";
		tab3ProdId = "";
		tab3Name = "";
		tab4ProdId = "";
		tab4Name = "";
		startDate = "";
		endDate = "";
		startTime = "";
		endTime = "";
	}

	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		clearErrorsAndMessages();
	}

	/**
	 * 從db讀取propertise資料
	 * 
	 * @param content
	 * @throws IOException
	 */
	void readPropertise(GenerateContent content) throws IOException {
		Properties prop = new Properties();
		Reader reader = new StringReader(content.getContents());
		prop.load(reader);
		reader.close();

		tab1ProdId = StringUtils.defaultString(prop.get("tab1ProdId").toString(), "");
		tab1Name = StringUtils.defaultString(prop.get("tab1Name").toString(), "");
		if (prop.containsKey("tab1adWord")) {
			tab1adWord = StringUtils.defaultString(prop.get("tab1adWord").toString(), "");
		}

		tab2ProdId = StringUtils.defaultString(prop.get("tab2ProdId").toString(), "");
		tab2Name = StringUtils.defaultString(prop.get("tab2Name").toString(), "");
		if (prop.containsKey("tab2adWord")) {
			tab2adWord = StringUtils.defaultString(prop.get("tab2adWord").toString(), "");
		}

		tab3ProdId = StringUtils.defaultString(prop.get("tab3ProdId").toString(), "");
		tab3Name = StringUtils.defaultString(prop.get("tab3Name").toString(), "");
		if (prop.containsKey("tab3adWord")) {
			tab3adWord = StringUtils.defaultString(prop.get("tab3adWord").toString(), "");
		}

		tab4ProdId = StringUtils.defaultString(prop.get("tab4ProdId").toString(), "");
		tab4Name = StringUtils.defaultString(prop.get("tab4Name").toString(), "");
		if (prop.containsKey("tab4adWord")) {
			tab4adWord = StringUtils.defaultString(prop.get("tab4adWord").toString(), "");
		}
	}

	/**
	 * 储存广告
	 * 
	 * @return
	 * @throws Exception
	 * @throws IOException
	 * @throws ParseException
	 * @throws CoreException
	 * @throws NumberFormatException
	 */
	public String saveIdxHotSaleAdv() throws NumberFormatException, CoreException, ParseException, IOException, Exception {
		boolean result = false;
		if (idxHotSaleAdvValidate()) {
			contents = new StringBuilder("");

			contents.append("tab1ProdId=").append(tab1ProdId).append("\n").append("tab1Name=").append(tab1Name).append("\n");
			if (StringUtils.isNotBlank(tab1adWord)) {
				contents.append("tab1adWord=").append(tab1adWord).append("\n");
			}
			contents.append("tab2ProdId=").append(tab2ProdId).append("\n").append("tab2Name=").append(tab2Name).append("\n");
			if (StringUtils.isNotBlank(tab2adWord)) {
				contents.append("tab2adWord=").append(tab2adWord).append("\n");
			}
			contents.append("tab3ProdId=").append(tab3ProdId).append("\n").append("tab3Name=").append(tab3Name).append("\n");
			if (StringUtils.isNotBlank(tab3adWord)) {
				contents.append("tab3adWord=").append(tab3adWord).append("\n");
			}
			contents.append("tab4ProdId=").append(tab4ProdId).append("\n").append("tab4Name=").append(tab4Name).append("\n");
			if (StringUtils.isNotBlank(tab4adWord)) {
				contents.append("tab4adWord=").append(tab4adWord).append("\n");
			}
			if (StringUtils.isBlank(advId)) {
				advertise = new GenerateAdvertise();
				position = new GeneratePosition();
				content = new GenerateContent();
			} else {
				advertise = generateService.get(Long.parseLong(advId));
				position = generateService.getGeneratePositionByAdvId(Long.parseLong(advId)).get(0);
				content = generateService.getGenerateContentByAdvId(Long.parseLong(advId)).get(0);
			}
			// set advertise
			advertise.setAdvType(advType);
			advertise.setAdvName(advName.trim());
			advertise.setStartDate(PATTERN_DATE_FORMAT.parse(startDate + " " + startTime + ":00"));
			advertise.setEndDate(PATTERN_DATE_FORMAT.parse(endDate + " " + endTime + ":59"));
			advertise.setAdvStatus(advStatus);
			content.setContents(contents.toString());

			if (StringUtils.countMatches(content.getContents(), "ProdId") == 4) {
				if (StringUtils.isBlank(advId)) {
					result = generateService.saveGenerateCombine(advertise, position, content);
				} else {
					result = generateService.updateGenerateCombine(advertise, position, content);
				}
			} else {
				result = false;
			}
			contents.setLength(0);
			// 存檔後立刻執行產生檔案的邏輯
			generateService.generateIdxHotSaleV2(null);
			if (!result) {
				return "editIdxHotSaleAdv";
			} else {
				advId = advertise.getId().toString();
				addActionMessage("储存成功");
			}
		}
		return editIdxHotSaleAdv();
	}

	/**
	 * @param advId the advId to set
	 */
	public void setAdvId(String advId) {
		this.advId = advId;
	}

	/**
	 * @param advName the advName to set
	 */
	public void setAdvName(String advName) {
		this.advName = advName;
	}

	/**
	 * @param advStatus the advStatus to set
	 */
	public void setAdvStatus(String advStatus) {
		this.advStatus = advStatus;
	}

	/**
	 * @param advType the advType to set
	 */
	public void setAdvType(String advType) {
		this.advType = advType;
	}

	/**
	 * @param content the content to set
	 */
	public void setContent(GenerateContent content) {
		this.content = content;
	}

	/**
	 * @param endDate the endDate to set
	 */
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	/**
	 * @param generateService the generateService to set
	 */
	public void setGenerateService(GenerateService generateService) {
		this.generateService = generateService;
	}

	/**
	 * @param startDate the startDate to set
	 */
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setTab1Name(String tab1Name) {
		this.tab1Name = tab1Name;
	}

	public void setTab1ProdId(String tab1ProdId) {
		this.tab1ProdId = tab1ProdId;
	}

	public void setTab2Name(String tab2Name) {
		this.tab2Name = tab2Name;
	}

	public void setTab2ProdId(String tab2ProdId) {
		this.tab2ProdId = tab2ProdId;
	}

	public void setTab3Name(String tab3Name) {
		this.tab3Name = tab3Name;
	}

	public void setTab3ProdId(String tab3ProdId) {
		this.tab3ProdId = tab3ProdId;
	}

	public void setTab4Name(String tab4Name) {
		this.tab4Name = tab4Name;
	}

	public void setTab4ProdId(String tab4ProdId) {
		this.tab4ProdId = tab4ProdId;
	}

	public String getTab1adWord() {
		return tab1adWord;
	}

	public void setTab1adWord(String tab1adWord) {
		this.tab1adWord = tab1adWord;
	}

	public String getTab2adWord() {
		return tab2adWord;
	}

	public void setTab2adWord(String tab2adWord) {
		this.tab2adWord = tab2adWord;
	}

	public String getTab3adWord() {
		return tab3adWord;
	}

	public void setTab3adWord(String tab3adWord) {
		this.tab3adWord = tab3adWord;
	}

	public String getTab4adWord() {
		return tab4adWord;
	}

	public void setTab4adWord(String tab4adWord) {
		this.tab4adWord = tab4adWord;
	}
}
